<?php
/**
 * 2019-09-11 16:45
 */
namespace app\admin\model;

use think\Model;

class Module extends Model{
    //不要制定表名，如果制定了，则不会自动添加从配置读取的前缀（反正类名即是表名）
    // protected $table = 'module';
    public function show(){
        return $this->select();
    }
    
    /*********
		2018/6/23
		获取模块列表
	************/
	public function getModuleList(){
        $rows = $this->where('module_type',1)->select();

		foreach($rows as $k=>$v){
			if(isset($v['icon'])){
				if($v['icon']){
					$rows[$k]['icon']='resources/images/icon/'.$v['icon'].'.png';
				}else{
					unset($rows[$k]['icon']);
				}
			}
		}
		$data = self::findChild($rows,0);
		$setting = array(
			"text"=>".",
			"children"=>$data
			
		);
		// var_dump($data);
		return $data;
	}

	/**********
		2018/6/23
		寻找子节点
	*************/
	public function findChild(&$rows,$pid){
		
		$data = array();
		foreach($rows as $item){
			if($item['module_parent_id'] == $pid){
				$children = self::findChild($rows,$item['module_id']);
				$item['leaf'] = (count($children) == 0);
				$item['expanded'] = true;
				$item['id'] = $item['module_id'];
				$item['url'] = $item['module_url'];
				$item['name'] = $item['module_name'];
				$item['children'] = $children;
				$data[] = $item;
			}
		}
		return $data;
	}
}